package com.gz.mylife.adapter;

import java.util.List;

import com.gz.mylife.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zl.smartmall.library.po.AssortFirstLevelInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AssortThirdLevelAdapter extends BaseAdapter {
	private Context mContext;
	private List<AssortFirstLevelInfo> infos;
	private LayoutInflater mInflater;
	private DisplayImageOptions mOptions;
	private int picWidth;

	public AssortThirdLevelAdapter(Context cxt, List<AssortFirstLevelInfo> lists) {
		this.mContext = cxt;
		this.infos = lists;
		mInflater = LayoutInflater.from(mContext);
		mOptions = creatOptions();
		WindowManager wm = (WindowManager) cxt.getSystemService(Context.WINDOW_SERVICE);
		int windowWidth = wm.getDefaultDisplay().getWidth();
		picWidth = (windowWidth-dip2px(cxt, 108))/3;
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}
	private DisplayImageOptions creatOptions() {
		return new DisplayImageOptions.Builder()
			.showImageOnFail(R.drawable.drawable_recent_white)
			.showImageForEmptyUri(R.drawable.drawable_recent_white)
			.showImageOnLoading(R.drawable.drawable_recent_white)
			.cacheInMemory(true)
			.cacheOnDisc(true)
			.build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return infos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.assortthirdlevel_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.pic = (ImageView) convertView.findViewById(R.id.iv_pic);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		AssortFirstLevelInfo info = infos.get(position);
		holder.title.setText(info.getName());
		ImageLoader.getInstance().displayImage(info.getPic(), holder.pic, mOptions);
		System.out.println(info.getPic());
		/*RelativeLayout.LayoutParams params = (LayoutParams) holder.pic.getLayoutParams();
		params.height = (int)(picWidth*1.2);
		params.width = picWidth;*/
		return convertView;
	}
	class ViewHolder{
		public TextView title;
		public ImageView pic;
	}
}
